// const moment = require('moment') // 时间格式化插件
const  Goods  = require('../collection/goods')
const { removeEmptyValues } = require('../../utils/methold')
module.exports = {
    add: async (ctx)=> {
        const  {
            goodsName,
            goodsImg,
            status,
            price,
            discount,
            des,
            weight,
            supplierPrice,
            supplierName,
            supplierPhone,
        } = ctx.request.body
        const item = await Goods.find({goodsName})
        if (item.length) {
            ctx.body = {
                status: 'error',
                msg: '该商品已存在,请勿重复添加'
            }
            return
        }
        const newGoods = new Goods({
            goodsName,
            goodsImg,
            status,
            price,
            discount,
            weight,
            supplierPrice,
            supplierName,
            supplierPhone,
            des})
        const  res =  await newGoods.save()
        const data =  {
            ...res._doc
        }
        data.goodsId = res._id
        delete data._id
        ctx.body = {
            status: 200,
            msg: '添加成功',
            data
        }
    },
    delete: async (ctx) =>{
        try {
            const  {
                goodsId,
            } = ctx.request.body
            if (!goodsId) {
                ctx.body = {
                    status: 'error',
                    msg: '必要id 不能为空!'
                }
            }
            const params = { _id: `${goodsId}` }
            const res = await Goods.deleteOne(params)
            if (res.deletedCount) {
                ctx.body = {
                    status: 200,
                    msg: '删除成功',
                    data: {}
                }
            } else {
                ctx.body = {
                    status: 200,
                    msg: '请勿重新删除',
                    data: {}
                }
            }
        }
        catch {
            ctx.body = {
                status: 200,
                msg: '请勿重新删除',
                data: {}
            }
        }
    },
    update: async (ctx) =>{
        try {
            const  {
                goodsId,
            } = ctx.request.body
            if (!goodsId) {
                ctx.body = {
                    status: 'error',
                    msg: '必要id 不能为空!'
                }
            }
            let params = {
                ...ctx.request.body
            }
            delete params.goodsId
            params = removeEmptyValues(params)
            const filter = { _id: `${goodsId}` }
            params.updateDate = new Date() // 修改时间更新下
            const res = await Goods.updateOne(filter,{ $set:params })
            if(res.acknowledged && res.modifiedCount) {
                ctx.body = {
                    status: 200,
                    data: {},
                    msg: '修改成功'
                }
            }else {
                ctx.body = {
                    status: 'error',
                    data: {},
                    msg: '数据异常'
                }
            }
        }
        catch(error){
            ctx.body = {
                status: 'error',
                data: {},
                msg: '修改失败'
            }
        }
    },
    getList: async (ctx) => {
        try {
            const  { pageSize, currentPage } = ctx.request.query
            let query = {
                ...ctx.request.query
            }
            delete query.pageSize
            delete query.currentPage
            query.hasOwnProperty('total')? delete query.total : ''
            // 清除空的字段
            query = removeEmptyValues(query)
            // 模糊匹配名称
            if(query.hasOwnProperty('goodsName')) {
                query.goodsName = {$regex: query.goodsName}
            }
            const list = await Goods.find(query).sort({ 'sortIndex': -1, 'createDate': -1 }).skip(pageSize * (currentPage - 1)).limit(pageSize)
            const total = await Goods.countDocuments(query);
            if (list) {
                ctx.body = {
                    status: 200,
                    data: {
                        list,
                        total,
                        pageSize: Number(pageSize),
                        currentPage: Number(currentPage)
                    },
                    msg: '成功'
                }
            } else {
                ctx.body = {
                    status: 200,
                    data: [],
                    msg: '成功'
                }
            }
        }
        catch(error) {
            ctx.body = {
                status: '',
                data: [],
                msg: '失败'
            }
        }
    },
    detail: async (ctx) => {
        try {
            const  { goodsId } = ctx.request.body
            console.log(goodsId,'goodsId')
            const params = { _id: `${goodsId}` }
            const item = await Goods.findOne(params)
            if (item) {
                ctx.body = {
                    status: 200,
                    data: item,
                    msg: '成功'
                }
            } else {
                ctx.body = {
                    status: 'error',
                    data: {},
                    msg: '查询失败'
                }
            }
        }
        catch(error) {
            ctx.body = {
                status: 'error',
                data: {},
                msg: '查询失败'
            }
        }

    },

}